package com.jianyun.jyzs.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jianyun.jyzs.R;
import com.jianyun.jyzs.bean.NewSign2Bean;
import com.jianyun.jyzs.utils.TextUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangshijun on 2018/7/30.
 */

public class NewSign2Adapter extends RecyclerView.Adapter<NewSign2Adapter.MyViewHolder> {


    private Context context;
    private List<NewSign2Bean.SignDetail> signDetail;
    private static final int BOTTOM_TYPE = 1;
    private static final int CONTENT_TYPE = 2;

    public NewSign2Adapter(Context context) {
        this.context = context;
        signDetail = new ArrayList<>();
    }

    public void setSignDetail(List<NewSign2Bean.SignDetail> signDetail) {
        this.signDetail = signDetail;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case CONTENT_TYPE:
                View view = View.inflate(context, R.layout.item_new_sign_2, null);
                return new MyViewHolder(view);
            case BOTTOM_TYPE:
                View view2 = View.inflate(context, R.layout.item_newsign2_bottom, null);
                return new MyViewHolder(view2);
        }
        return null;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount() - 1) {
            return BOTTOM_TYPE;
        } else {
            return CONTENT_TYPE;
        }
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        if (position == getItemCount() - 1) {

        } else {
            if (position == signDetail.size() - 1) {
                //holder.view.setVisibility(View.INVISIBLE);
            }
            final NewSign2Bean.SignDetail signDetail = this.signDetail.get(position);
            String workType = signDetail.getWorkType();

            if ("1".equals(workType)) {
                holder.tvType.setText("签到");
                holder.tvLocation.setText("请在" + signDetail.getSignCriticalTime() + "之后签到");
                holder.tvButton.setText("签到");
            } else if ("2".equals(workType)) {
                holder.tvType.setText("签退");
                holder.tvLocation.setText("请在" + signDetail.getSignCriticalTime() + "之前签退");
                holder.tvButton.setText("签退");
            }
            holder.tvTimeStandard.setText(signDetail.getWorkTime());
            String signStatusCode = signDetail.getSignStatusCode();
            String signAddressStatusCode = signDetail.getSignAddressStatusCode();
            boolean isShowSignStatusCode = signDetail.getIsShowSignStatusCode();
            boolean isNextDayXb = signDetail.getIsNextDayXb();
            String signTime = signDetail.getSignTime();
            //截取
            String subSignTime = "";
            if (!TextUtils.isEmpty(signTime) && signTime.length() > 6) {
                String subSignTimea = signTime.substring(signTime.lastIndexOf(" ") + 1);
                subSignTime = subSignTimea.substring(0, subSignTimea.lastIndexOf(":"));
            } else {
                subSignTime = signTime;

            }
            String signAddress = signDetail.getSignAddress();
            String subAddress = "";
            if (!TextUtils.isEmpty(signAddress)) {
                if (signAddress.length() > 10) {
                    //subAddress = signAddress.substring(0, 10) + "...";
                    subAddress = signAddress;
                }else {
                    subAddress=signAddress;
                }
            }
            if (isNextDayXb) {
                holder.netDayIcon.setVisibility(View.VISIBLE);
            } else {
                holder.netDayIcon.setVisibility(View.INVISIBLE);
            }
            switch (signStatusCode) {
                case "1":
                    holder.imgIcon.setVisibility(View.VISIBLE);
                    holder.tvLocation.setText(subAddress);
                    holder.tvSignType.setText("正常");
                    if (isShowSignStatusCode){
                        holder.tvSignType.setVisibility(View.VISIBLE);
                    }else {
                        holder.tvSignType.setVisibility(View.INVISIBLE);
                    }

                    holder.tvSignType.setTextColor(context.getResources().getColor(R.color.bacck333));
                    holder.tvButton.setText(subSignTime);
                    holder.tvButton.setTextColor(context.getResources().getColor(R.color.tv_grade));
                    holder.btLl.setBackground(context.getResources().getDrawable(R.drawable.ll_line_blue));
                    break;
                case "2":
                    holder.imgIcon.setVisibility(View.VISIBLE);
                    holder.tvLocation.setText(subAddress);
                    holder.tvSignType.setText("迟到");
                    if (isShowSignStatusCode){
                        holder.tvSignType.setVisibility(View.VISIBLE);
                    }else {
                        holder.tvSignType.setVisibility(View.INVISIBLE);
                    }
                    holder.tvSignType.setTextColor(context.getResources().getColor(R.color.ff33));
                    holder.tvButton.setText(subSignTime);
                    holder.tvButton.setTextColor(context.getResources().getColor(R.color.ff33));
                    holder.btLl.setBackground(context.getResources().getDrawable(R.drawable.ll_line_recd));
                    break;
                case "3":
                    holder.imgIcon.setVisibility(View.VISIBLE);
                    holder.tvLocation.setText(subAddress);
                    holder.tvSignType.setText("早退");
                    if (isShowSignStatusCode){
                        holder.tvSignType.setVisibility(View.VISIBLE);
                    }else {
                        holder.tvSignType.setVisibility(View.INVISIBLE);
                    }
                    holder.tvSignType.setTextColor(context.getResources().getColor(R.color.ff33));
                    holder.tvButton.setText(subSignTime);
                    holder.tvButton.setTextColor(context.getResources().getColor(R.color.ff33));
                    holder.btLl.setBackground(context.getResources().getDrawable(R.drawable.ll_line_recd));
                    break;
                case "4":
                    holder.imgIcon.setVisibility(View.INVISIBLE);
                    holder.tvSignType.setText("未打卡");
                    holder.tvSignType.setVisibility(View.INVISIBLE);
                    holder.tvSignType.setTextColor(context.getResources().getColor(R.color.ff33));
                    holder.tvButton.setTextColor(context.getResources().getColor(R.color.tv_grade));
                    holder.btLl.setBackground(context.getResources().getDrawable(R.drawable.ll_line_blue));
                    break;
                case "5":
                    //未到打卡时间
                    holder.imgIcon.setVisibility(View.INVISIBLE);
                    holder.tvSignType.setVisibility(View.INVISIBLE);
                    holder.tvButton.setTextColor(context.getResources().getColor(R.color.tv_grade));
                    holder.tvLocation.setTextColor(context.getResources().getColor(R.color.back999));
                    holder.btLl.setBackground(context.getResources().getDrawable(R.drawable.ll_line_blue));
                    break;
                default:
                    break;

            }
            if (null != signAddressStatusCode && !TextUtils.isEmpty(subAddress)) {
                if ("1".equals(signAddressStatusCode)) {
                    holder.tvLocation.setTextColor(context.getResources().getColor(R.color.bacck333));
                } else if ("2".equals(signAddressStatusCode)) {
                    holder.tvLocation.setTextColor(context.getResources().getColor(R.color.ff33));
                }

            }
            holder.btLl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickListener.onclick(position, signDetail);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return signDetail.size() + 1;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private View view;
        TextView tvType;
        TextView tvTimeStandard;
        ImageView imgIcon;
        TextView tvLocation;
        TextView tvSignType;
        TextView tvButton;
        LinearLayout btLl;
        ImageView netDayIcon;

        public MyViewHolder(View itemView) {
            super(itemView);
            view = itemView.findViewById(R.id.view_line);
            tvType = itemView.findViewById(R.id.tv_type);
            tvTimeStandard = itemView.findViewById(R.id.tv_time_standard);
            imgIcon = itemView.findViewById(R.id.img_icon);
            tvLocation = itemView.findViewById(R.id.tv_location);
            tvSignType = itemView.findViewById(R.id.tv_sign_type);
            tvButton = itemView.findViewById(R.id.tv_button);
            btLl = itemView.findViewById(R.id.ll_apply);
            netDayIcon = itemView.findViewById(R.id.img_next_day);
        }
    }

    private OnClickListener onClickListener;

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public interface OnClickListener {
        void onclick(int postion, NewSign2Bean.SignDetail signDetail);
    }
}
